<?php

namespace backend\controllers;

use yii\web\Controller;
use backend\extention_class\zhuji;
use backend\models\DeliveryinfoForm;
use backend\models\TbaDeliveryinfo;

/**
 * #name=配送商管理#
 *
 *
 */
class DeliveryinfoController extends Controller {
	
	public  $layout = 'ace';   //定义布局
	public $enableCsrfValidation = false;
	
	
	/**
	 * #operation=配送商列表#
	 */
	public  function  actionIndex(){
	
		//生成助记码
		if(\yii::$app->request->isPost && \yii::$app->request->post('action') == 'zhuji' ){
			$value = \yii::$app->request->post('value');	//需要生成助记码的值
			$zhuji = new zhuji();   						//实例化助记码
			echo strtolower($zhuji->getInitials($value));	//转化为小写字母
			exit;
		}
		
		//处理表单提交过来的值
		if(\yii::$app->request->post('action') && \yii::$app->request->post('action') == 'add' ){
		
			$derveryModel = new DeliveryinfoForm(['scenario'=>'add']);    //实例化物流商表单模型
			$derveryModel->attributes = \yii::$app->request->post('DeliveryinfoForm');
			if($derveryModel->validate()){
				$dervery = new TbaDeliveryinfo();						  //实例化物流商AR模型
				$dervery->DeliveryID = $derveryModel->deliveryAb;         //助记码
				$dervery->DeliveryName = $derveryModel->deliveryName;	  //物流商名
				$dervery->Address = $derveryModel->address;				  //地址
				$dervery->LinkName = $derveryModel->linkName;			  //联系人
				$dervery->LinkTel = $derveryModel->linkTel;				  //联系电话
				$dervery->QQ = $derveryModel->qq;						  //QQ
				$dervery->Email = $derveryModel->email;					  //邮箱
				$dervery->SortID = 1;									  //排序码
				$dervery->State = ($derveryModel->state !== null )?1:0;
				if($dervery->save()){
					exit('<script>alert("添加成功");window.history.go(-1);</script>');
				}else{
					exit('<script>alert("添加失败");window.history.go(-1);</script>');
				}
			}
								
			exit;
		}
				
		$model = new DeliveryinfoForm(['scenario'=>'add']);   //实例化物流商表单模型
		return  $this->render('index',['model'=>$model]);
				
	}
	
	
	/**
	 * #operation=配送商列表数据源#
	 */
	public function actionDatasource(){
	
		return $this->renderPartial('datasource');
		
	}
	
	
	/**
	 * #operation=订单修改#
	 */
	public function actionAlter(){
		
		//生成助记码
		if(\yii::$app->request->post('action') && \yii::$app->request->post('action') == 'zhuji' ){
			$value = \yii::$app->request->post('value');	//需要生成助记码的值			
			$zhuji = new zhuji();  							//实例化助记码
			echo strtolower($zhuji->getInitials($value));	//转化为小写字母
			exit;
		}
		
		//判断是否是展现修改界面的动作
		if(\yii::$app->request->post('alterid') && \yii::$app->request->post('action') == "view" ){
			
			$id = \yii::$app->request->post('alterid');       //物流商ID
			$deliveryinfo = TbaDeliveryinfo::find()->where(['ID'=>$id])->asArray()->one();   //查询物流商表
			$deliveryID = $deliveryinfo['DeliveryID'];        //物流商助记码
			$deliveryName = $deliveryinfo['DeliveryName'];    //物流商名
			$linkName = $deliveryinfo['LinkName'];			  //联系人
			$linkTel = $deliveryinfo['LinkTel'];			  //联系电话
			$address = $deliveryinfo['Address'];			  //地址
			$qq = $deliveryinfo['QQ'];						  //QQ
			$email = $deliveryinfo['Email'];				  //邮箱
			$state = $deliveryinfo['State'];				  //状态
				
		}
		
		//处理表单提交过来的值
		if(\yii::$app->request->isPost && \yii::$app->request->post('action') == "alter" ){
			$id = \yii::$app->request->post('alterid');   	 			   //物流商ID
			$derveryModel = new DeliveryinfoForm(['scenario'=>'add']);     //实例化物流商表单模型
			$derveryModel->attributes = \yii::$app->request->post('DeliveryinfoForm');   //给表单模型赋值
			if($derveryModel->validate()){
				$dervery = TbaDeliveryinfo::findOne(['ID'=>$id]);		   //实例化物流商AR模型
				$dervery->DeliveryID = $derveryModel->deliveryAb;          //助记码
				$dervery->DeliveryName = $derveryModel->deliveryName;	   //物流商名
				$dervery->Address = $derveryModel->address;				   //地址
				$dervery->LinkName = $derveryModel->linkName;			   //联系人
				$dervery->LinkTel = $derveryModel->linkTel;				   //联系电话
				$dervery->QQ = $derveryModel->qq;						   //QQ
				$dervery->Email = $derveryModel->email;					   //邮箱
				
				$dervery->State = ($derveryModel->state !== null )?1:0;    //物流商状态
				if($dervery->save()){
					exit('<script>alert("修改成功");window.history.go(-1);</script>');
				}else{
					exit('<script>alert("修改失败");window.history.go(-1);</script>');
				}
				
			}else{
				
				$errors = $derveryModel->errors;  						  //错误信息
				$temp ="";
				foreach ($errors as $v) {
					foreach ($v as $p ){
						$temp .= $p."，";
					}
				}
				exit("<script>alert('".$temp."');window.history.go(-1);</script>");  //打印错误信息
			}
			
			exit;
			
		}
		
		$model = new DeliveryinfoForm(['scenario'=>'add']);	  //实例化物流商表单模型,传递参数变量
		return $this->renderPartial('alters',[  'model'=>$model,
												'deliveryID'=>$deliveryID,
												'deliveryName'=>$deliveryName,
												'linkName'=>$linkName,
												'linkTel'=>$linkTel,
												'address'=>$address,
												'qq'=>$qq,
												'email'=>$email,
												'state'=>$state,
												'id'=>$id
								  ]);
		
	}
	
	
	/**
	 * #operation=配送商删除#
	 */
	public function actionDelet(){
	
		//判断是否是单条删除动作
		if(\yii::$app->request->post("action") == "delet"){
			$id = \yii::$app->request->post('deliveryid');  	  				 //物流商ID
			$delivery = TbaDeliveryinfo::findOne(['ID'=>$id]);	  				 //查询物流商表	
	
			if($delivery->delete()){			
				echo json_encode('删除成功');
				exit;
			}else{
				echo json_encode('删除失败');
				exit;
			}
	
		}
	
		//判断是否是全部删除
		if(\yii::$app->request->post("action") == "deletall"){
				
			if(\yii::$app->request->post('check')){
				$check = \yii::$app->request->post('check');
				
				foreach ($check as $v ){
					$deliveryinfo = TbaDeliveryinfo::findOne(['ID'=>$v]);      //查询物流商表
					if(!$deliveryinfo->delete())
						exit("<script>alert('删除失败');window.history.go(-1);</script>");
				}
				exit("<script>alert('删除成功');window.history.go(-1);</script>");
	
			}else{
				exit("<script>alert('请选择后在删除');window.history.go(-1);</script>");
			}
		}
	}
	
	
	
	
	
}

?>